<?php

namespace App\Models\Factory;

use App\Models\AbsModelFactory;
use App\Models\Orm\DataProductCount;
use App\Models\Orm\DataUserCount;
use Illuminate\Support\Facades\DB;

/**
 * 产品数据统计工厂方法
 *
 * Class DataProductCountFactory
 * @package App\Models\Factory
 */
class DataUserCountFactory extends AbsModelFactory
{
    /**
     * 获取单日产品总览
     * @return array
     */
    public static function getDayDataUserCount($date)
    {
        $res = DataUserCount::select([
            'app_name',
            'register_pv',
            'register_uv',
            'login_pv',
            'login_uv',
            'register_total',
            'login_total',

        ])
            ->where('create_date', $date)
            ->orderBy('app_name')
            ->get();

        return $res ?: [];
    }

    /**
     * 获取30天产品总览
     * @return array
     */
    public static function getMonthDataUserCount()
    {
//        $start = date('Y-m-d', strtotime('-31 day'));
//        $end = date('Y-m-d', strtotime('-1 day'));

        $res = DataUserCount::select([
            DB::raw('sum(register_total) as register_total'),
            DB::raw('sum(login_total) as login_total'),
            DB::raw('sum(register_pv) as register_pv'),
            DB::raw('sum(register_uv) as register_uv'),
            DB::raw('sum(login_pv) as login_pv'),
            DB::raw('sum(login_uv) as login_uv'),
            'app_name'
        ])
//            ->where('create_date', '>', $start)
//            ->where('create_date', '<=', $end)
            ->groupby('app_name')
            ->get();

        return $res ?: [];
    }

    /**
     * 获取某产品统计列表
     *
     * @param $product_id
     * @return mixed
     */
    public static function getUserCountList($app_name)
    {
        $res = DataUserCount::select('*')
            ->where('app_name', $app_name)
            ->get();
        return $res;
    }
}
